<template>
  <div class="auth-container">
    <div class="auth-card">
      <div class="auth-header">
        <h2>用户登录</h2>
        <p>欢迎回到威龙数字图书馆</p>
      </div>

      <form class="auth-form" @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            id="email"
            type="email"
            v-model="form.email"
            required
            placeholder="请输入注册邮箱"
          >
          <div class="underline"></div>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            type="password"
            v-model="form.password"
            required
            placeholder="请输入密码"
            minlength="6"
          >
          <div class="underline"></div>
        </div>

        <!-- 新增管理员登录切换 -->
        <div class="form-group admin-mode-toggle">
          <div class="toggle-switch">
            <input type="checkbox" id="admin-mode" v-model="isAdminLogin" hidden>
            <label class="toggle-label" for="admin-mode"></label>
          </div>
          <label for="admin-mode">使用管理员账号登录</label>
        </div>

        <button type="submit" :disabled="isSubmitting" class="auth-button">
          <span v-if="!isSubmitting">立即登录</span>
          <span v-else class="loading">登录中...</span>
        </button>

        <div class="auth-links">
          <router-link to="/signup" class="auth-link">没有账号？立即注册</router-link>
          <router-link to="/forgot-password" class="auth-link">忘记密码？</router-link>
        </div>

        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        email: '',
        password: '',
        adminID:'',
      },
      isAdminLogin: 0, // 是否为管理员登录，默认为普通用户登录
      isSubmitting: false,
      errorMessage: '',
    }
  },

  methods: {
    async handleSubmit() {
      try {
        this.isSubmitting = true;
        this.errorMessage = '';

        // 构造请求数据
        const userData = {
          email: this.form.email,
          password: this.form.password
        };

        // 根据是否为管理员登录选择不同的请求地址
        const loginUrl = this.isAdminLogin 
          ? 'http://8.134.212.44:9999/admin/login' 
          : 'http://8.134.212.44:9999/user/login';

        const response = await fetch(`${loginUrl}?email=${encodeURIComponent(userData.email)}&password=${encodeURIComponent(userData.password)}`, {
        method: 'GET',
        headers: {
          'Accept': 'application/json'
        }
        });

        
        // 处理响应
        if (!response.ok) {
          throw new Error(`登录失败: ${response.statusText}`);
        }

        const result = await response.json();
        const { email, name ,Id} = result.data;
        
        // 根据登录模式设置 isAdmin 标识
        const isAdminValue = this.isAdminLogin ? 1 : 0;

        // 存储到本地存储
        localStorage.setItem('user', JSON.stringify({
          isLoggedIn: true,
          email,
          name,
          isAdmin: isAdminValue, 
          Id,
        }));

        // 触发全局登录事件
        this.$emitter.emit('user-login');

        // 跳转到个人主页
        this.$router.push({ name: 'personal_homepage' });

      } catch (error) {
        console.error('登录错误:', error);
        this.handleError(error);
      } finally {
        this.isSubmitting = false;
      }
    },

    handleError(error) {
      localStorage.removeItem('user');
      
      if (error.message.includes('401')) {
        this.errorMessage = '邮箱或密码错误';
      } else if (error.message.includes('网络')) {
        this.errorMessage = '网络连接异常，请检查网络设置';
      } else {
        this.errorMessage = '登录失败，请稍后重试';
      }
    }
  }
}
</script>

<style scoped>
:root {
  --primary-color: #42b983;
  --secondary-color: #2c3e50;
  --accent-color: #f5a623;
  --error-color: #e74c3c;
  --text-light: #f8f9fa;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}



.admin-mode-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 1rem;
}

.toggle-switch {
  position: relative;
  width: 50px;
  height: 24px;
}

.toggle-label {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  border-radius: 34px;
  transition: .4s;
}

.toggle-label:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  /* bottom: 0px; */
  background-color: rgb(255, 255, 255);
  border-radius: 50%;
  transition: .4s;
}

input:checked + .toggle-label {
  background-color: #42b983;
}

input:checked + .toggle-label:before {
  transform: translateX(26px);
}




.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;
}

.auth-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  opacity: 0.2;
  z-index: -1;
}

.auth-card {
  background: white;
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 3rem;
  width: 100%;
  max-width: 400px;
  text-align: center;
  position: relative;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.auth-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 12px 20px rgba(0, 0, 0, 0.15);
}

.auth-header {
  margin-bottom: 2.5rem;
}

.auth-header h2 {
  color: var(--secondary-color);
  margin-bottom: 0.5rem;
  font-weight: 700;
}

.auth-header p {
  color: #7f8c8d;
  font-size: 1rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 1.8rem;
  position: relative;
  text-align: left;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--secondary-color);
  font-weight: 600;
}

.form-group input {
  width: 100%;
  padding: 0.8rem 0;
  font-size: 1rem;
  border: none;
  border-bottom: 2px solid #ddd;
  outline: none;
  background: transparent;
  transition: all 0.3s;
}

.form-group input:focus {
  border-bottom-color: var(--primary-color);
}

.form-group input::placeholder {
  color: #bdc3c7;
  transition: all 0.3s;
}

.form-group input:focus::placeholder {
  color: transparent;
}

.form-group .underline {
  height: 3px;
  width: 100%;
  background: var(--primary-color);
  position: absolute;
  bottom: 0;
  left: 0;
  transform-origin: left;
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.form-group input:focus + .underline {
  transform: scaleX(1);
}

.auth-button {
  background: var(--primary-color);
  color: rgb(18, 168, 179);
  border: none;
  padding: 0.8rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 0.5rem;
  position: relative;
  overflow: hidden;
}

.auth-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: all 0.5s;
}

.auth-button:hover::before {
  left: 100%;
}

.auth-button:hover {
  background: #fae73c;
  transform: translateY(-3px);
  box-shadow: 0 6px 12px rgba(66, 185, 131, 0.3);
}

.auth-button:disabled {
  background: #95a5a6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.auth-links {
  display: flex;
  justify-content: space-between;
  margin-top: 2rem;
  margin-bottom: 1.5rem;
}

.auth-link {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.auth-link::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: -2px;
  left: 0;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.auth-link:hover::after {
  width: 100%;
}

.auth-link:hover {
  color: #3aa373;
}

.error-message {
  background-color: #ffecec;
  color: var(--error-color);
  padding: 1rem;
  border-radius: 8px;
  margin-top: 1.5rem;
  text-align: center;
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-5px); }
}

.loading::after {
  content: '';
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-left: 8px;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: white;
  animation: loading 1s linear infinite;
}

@keyframes loading {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 添加一些装饰元素 */
.auth-card::before {
  content: '';
  position: absolute;
  top: -20px;
  right: -20px;
  width: 60px;
  height: 60px;
  background: var(--primary-color);
  border-radius: 50%;
  opacity: 0.1;
  z-index: -1;
}

.auth-card::after {
  content: '';
  position: absolute;
  bottom: -20px;
  left: -20px;
  width: 80px;
  height: 80px;
  background: var(--accent-color);
  border-radius: 50%;
  opacity: 0.1;
  z-index: -1;
}

@media (max-width: 480px) {
  .auth-container {
    padding: 0 1rem;
  }
  
  .auth-card {
    padding: 2rem;
  }
}
</style>